/**
 * @brief 测试VideoCapture动态切换视频源
 */
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include <string>
#include <vector>

#include "env.h"

int main(void) {
    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;

    vector<string> play_list{kResourcesDir + "test01.mp4", kResourcesDir + "test02.mp4"};

    std::size_t index = 0;
    std::size_t total = play_list.size();

    auto cap = cv::VideoCapture(play_list[index]);

    if (cap.isOpened()) {
        cout << "open videocapture successful!" << '\n';
    } else {
        cout << "failed!" << '\n';
    }

    const string title = "play video";

    cv::namedWindow(title);

    // cap.set(cv::CAP_PROP_FPS, 30);

    while (true) {
        cv::Mat image;

        cap >> image;

        cv::imshow(title, image);

        auto key = cv::waitKey(30);

        if (key == 'q') {
            break;
        }
        if (key == 's') {
            // 关闭视频文件或捕获文件，该接口在对象析构或再次调用open时自动调用
            // cap.release();
            index = (index + 1) % total;
            cap.open(play_list[index]);
            if (!cap.isOpened()) {
                break;
            }
        }
    }

    cv::destroyWindow(title);
    cap.release();

    return 0;
}
